package com.three.digit.chart;

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JPanel;
import javax.swing.JTextField;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.axis.NumberTickUnit;
import org.jfree.chart.axis.SymbolAxis;
import org.jfree.chart.labels.XYItemLabelGenerator;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;
import org.jfree.ui.tabbedui.VerticalLayout;

import com.three.digit.model.Point;
import com.three.digit.tools.Utils;

@SuppressWarnings ("serial")
public class DefaultChart extends ApplicationFrame
{

    public static final String HIS_DATA_PATH = "BackUpSanDHistory.txt";
    public static List <Point> HIS_LIST = Utils.readPoints (HIS_DATA_PATH);
    public static HashMap <Integer, Point> HIS_MAP = Utils.toHashMap (HIS_LIST);

    public int size = 20;
    private XYDataset xyDataset;

    public DefaultChart (String title)
    {
        super (title);

        this.xyDataset = createDataset ();

        JPanel localJPanel = createDemoPanel ();
        localJPanel.setPreferredSize (new Dimension (1000, 700));

        setLayout (new VerticalLayout (true));
        add (createOperArea ());
        add (localJPanel);
    }

    public JPanel createDemoPanel ()
    {
        JFreeChart localJFreeChart = createChart (xyDataset);

        ChartPanel localChartPanel = new ChartPanel (localJFreeChart);
        localChartPanel.setMouseWheelEnabled (false);

        return localChartPanel;
    }

    private XYDataset createDataset ()
    {
        XYSeries localXYSeries1 = new XYSeries ("sum");

        for (int i = 0; i < size; i++)
        {
            Point point = HIS_LIST.get (i);
            localXYSeries1.add (point.getId (), (double) point.sum ());
        }

        XYSeries localXYSeries2 = new XYSeries ("span");

        for (int i = 0; i < size; i++)
        {
            Point point = HIS_LIST.get (i);
            localXYSeries2.add (point.getId (), (double) point.span ());
        }

        XYSeriesCollection localXYSeriesCollection = new XYSeriesCollection ();
        localXYSeriesCollection.addSeries (localXYSeries1);
        localXYSeriesCollection.addSeries (localXYSeries2);
        return localXYSeriesCollection;
    }

    private JFreeChart createChart (XYDataset paramXYDataset)
    {
        JFreeChart localJFreeChart = ChartFactory.createXYLineChart ("3D cross plot", "X", "Y", paramXYDataset,
                                                                     PlotOrientation.VERTICAL, true, true, false);
        XYPlot localXYPlot = (XYPlot) localJFreeChart.getPlot ();

        localXYPlot.setDomainPannable (true);
        localXYPlot.setRangePannable (true);
        XYLineAndShapeRenderer localXYLineAndShapeRenderer = (XYLineAndShapeRenderer) localXYPlot.getRenderer ();
        localXYLineAndShapeRenderer.setBaseShapesVisible (true);
        localXYLineAndShapeRenderer.setBaseShapesFilled (true);
        localXYLineAndShapeRenderer.setBaseItemLabelsVisible (true);
        localXYLineAndShapeRenderer.setBaseItemLabelGenerator (new XYItemLabelGenerator ()
        {

            public String generateLabel (XYDataset dataset, int series, int item)
            {
                if (series == 0)
                {
                    int x = dataset.getX (series, item).intValue ();
                    Point point = HIS_MAP.get (x);

                    String text = point.getX () + "," + point.getY () + "," + point.getZ ();

                    return dataset.getY (series, item).intValue () + "(" + text + ")";
                }
                else
                {
                    return dataset.getY (series, item).intValue () + "";
                }
            }
        });

        NumberAxis localNumberAxis = (NumberAxis) localXYPlot.getRangeAxis ();
        localNumberAxis.setStandardTickUnits (NumberAxis.createIntegerTickUnits ());
        localNumberAxis.setTickUnit (new NumberTickUnit (1));
        localNumberAxis.setRange (0, 27);

        // NumberAxis domainAxis = (NumberAxis) localXYPlot.getDomainAxis();
        //
        // NumberFormat numberFormat = NumberFormat.getIntegerInstance();
        //
        // domainAxis.setTickUnit(new NumberTickUnit(1, numberFormat));
        // domainAxis.setVerticalTickLabels(true);

        SymbolAxis symbolAxis = new SymbolAxis ("text", new String[]
        { "2004001", "2004002" });

        localXYPlot.setDomainAxis (symbolAxis);

        // localXYPlot.setDomainAxis(2, symbolAxis);

        return localJFreeChart;
    }

    public JPanel createOperArea ()
    {
        final JTextField textField = new JTextField ("aaaa");

        final JComboBox jComboBox = new JComboBox ();
        jComboBox.addItem ("2004001");
        jComboBox.addItem ("2004002");
        jComboBox.setSize (75, 25);

        JButton jButton = new JButton ("下一组");
        jButton.addActionListener (new ActionListener ()
        {
            int count = 0;

            public void actionPerformed (ActionEvent e)
            {
                count++;
                XYSeriesCollection xySeriesCollection = (XYSeriesCollection) xyDataset;

                updateSeries (xySeriesCollection.getSeries (0), "sum");
                updateSeries (xySeriesCollection.getSeries (1), "span");
            }

            public void updateSeries (XYSeries xySeries, String type)
            {
                while (xySeries.getItemCount () > 0)
                {
                    xySeries.remove (0);
                }

                for (int i = 0; i < size; i++)
                {
                    if ((i + size * count) < HIS_LIST.size ())
                    {
                        Point point = HIS_LIST.get (i + size * count);

                        if ("span".equals (type))
                        {
                            xySeries.add (point.getId (), point.span ());
                        }
                        else if ("sum".equals (type))
                        {
                            xySeries.add (point.getId (), point.sum ());
                        }
                    }
                }
            }
        });

        JPanel operContain = new JPanel (new FlowLayout ());
        operContain.add (textField);
        operContain.add (jComboBox);
        operContain.add (jButton);

        XYSeriesCollection xySeriesCollection = (XYSeriesCollection) xyDataset;
        for (int i = 0; i < xySeriesCollection.getSeriesCount (); i++)
        {
            operContain.add (new D3JCheckBox (xySeriesCollection.getSeries (i), xySeriesCollection));
        }

        return operContain;
    }

    /**
     * @param args
     */
    public static void main (String[] args)
    {
        DefaultChart localLineChartDemo2 = new DefaultChart ("JFreeChart: LineChartDemo2.java");
        localLineChartDemo2.pack ();
        RefineryUtilities.centerFrameOnScreen (localLineChartDemo2);
        localLineChartDemo2.setVisible (true);
    }

}
